//
//  HLSettingPasswordViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLSettingPasswordViewController.h"
#import "HLUserOptionRequest.h"
#import "HLLoginOptionRequest.h"

@interface HLSettingPasswordViewController ()<QMUITextFieldDelegate>

@property (nonatomic, strong) QMUIButton *saveButton;
@property (nonatomic, strong) QMUITextField *textField;
@property (nonatomic, strong) QMUITextField *codeField;
@property (nonatomic, strong) QMUIButton *codeButton;
@property (nonatomic, strong) QMUITextField *passwordField;
@property (nonatomic, strong) QMUITextField *confirmPasswordField;
@property (nonatomic, weak) NSTimer *countdownTimer;

@end

@implementation HLSettingPasswordViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HLBackgroundColor;
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    self.title = @"设置密码";
}

- (void)initSubviews
{
    [super initSubviews];

    self.textField = [[QMUITextField alloc] init];
    _textField.placeholder = @"请输入手机号或邮箱";
    _textField.font = UIFontMake(14);
    _textField.textColor = HLTextColor1;
    _textField.backgroundColor = UIColorWhite;
    _textField.delegate = self;
    _textField.textInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    [self.view addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(NavigationContentTopConstant + 12);
        make.height.mas_equalTo(60);
    }];
    
    self.codeField = [[QMUITextField alloc] init];
    _codeField.placeholder = @"请输入验证码";
    _codeField.font = UIFontMake(14);
    _codeField.textColor = HLTextColor1;
    _codeField.backgroundColor = UIColorWhite;
    _codeField.textInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    _codeField.maximumTextLength = 6;
    [self.view addSubview:_codeField];
    [_codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_textField.mas_bottom);
        make.height.equalTo(_textField);
    }];
    
    self.codeButton = [[QMUIButton alloc] init];
    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_codeButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_codeButton.titleLabel setFont:UIFontMake(15)];
    [_codeButton setBackgroundColor:HLMainColor];
    [_codeButton setContentEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    [_codeButton addTarget:self action:@selector(SMSButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    _codeButton.cornerRadius = 4;
    _codeButton.enabled = NO;
    [self.view addSubview:_codeButton];
    [_codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_codeField);
        make.right.mas_equalTo(-16);
    }];
    
    self.passwordField = [[QMUITextField alloc] init];
    _passwordField.placeholder = @"请输入6～20位的数字和字母新密码";
    _passwordField.font = UIFontMake(14);
    _passwordField.textColor = HLTextColor1;
    _passwordField.backgroundColor = UIColorWhite;
    _passwordField.secureTextEntry = YES;
    _passwordField.maximumTextLength = 20;
    _passwordField.textInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    [self.view addSubview:_passwordField];
    [_passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_codeField.mas_bottom);
        make.height.equalTo(_textField);
    }];
    
    
    self.confirmPasswordField = [[QMUITextField alloc] init];
    _confirmPasswordField.placeholder = @"请确认密码";
    _confirmPasswordField.font = UIFontMake(14);
    _confirmPasswordField.textColor = HLTextColor1;
    _confirmPasswordField.backgroundColor = UIColorWhite;
    _confirmPasswordField.secureTextEntry = YES;
    _confirmPasswordField.maximumTextLength = 20;
    _confirmPasswordField.textInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    [self.view addSubview:_confirmPasswordField];
    [_confirmPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_passwordField.mas_bottom);
        make.height.equalTo(_textField);
    }];
    
    self.saveButton = [[QMUIButton alloc] init];
    _saveButton.titleLabel.font = UIFontMake(15);
    [_saveButton setTitle:@"确认" forState:UIControlStateNormal];
    [_saveButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_saveButton setBackgroundColor:HLMainColor];
    _saveButton.cornerRadius = 4;
    [_saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_saveButton];
    [_saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(52);
        make.bottom.mas_equalTo(- (50 + SafeAreaInsetsConstantForDeviceWithNotch.bottom));
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (void)saveAction
{
    NSString *account = self.textField.text;
    NSString *code = self.codeField.text;
    NSString *password = self.passwordField.text;
    NSString *passwordConfirm = self.confirmPasswordField.text;
    
    
    if (account.length < 1){
        [QMUITips showWithText:@"请输入手机号或邮箱"];
        return;
    }
    
    if (code.length < 1){
        [QMUITips showWithText:@"请输入验证码"];
        return;
    }
    
    if (password.length < 6){
        [QMUITips showWithText:@"请输入6～20位的数字和字母密码"];
        return;
    }
    
    if (![password isEqualToString:passwordConfirm]){
        [QMUITips showWithText:@"两次输入密码不一样"];
        return;
    }
    
    HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypePassword params:@{@"account":account, @"code":code, @"password":password, @"scene":@"MODIFY_PWD"}];
    [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
        if (request.isSuccess){
            [HLHelper logout];
            [HLUIHelper setRootLogin];
        }
    } failure:nil];
}


- (void)SMSButtonDidClicked:(QMUIButton *)button
{
    NSString *account = [self.textField text];
    
    HLLoginOptionType type;
    NSDictionary *params = @{};
    if ([account containsString:@"@"]) {
        type = HLLoginOptionTypeSendMailCode;
        params = @{@"account":account,@"scene":@"MODIFY_PWD"};
    } else {
        type = HLLoginOptionTypeSendSmsCode;
        params = @{@"account":account,@"scene":@"MODIFY_PWD"};
    }
    HLLoginOptionRequest *userOptionRequest = [[HLLoginOptionRequest alloc] initWithType:type params:params];
    [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
        if (request.isSuccess){
            [QMUITips showWithText:@"验证码已发送"];
            [self startCountdownTimerIfNecessary];
        } else {
            [QMUITips showWithText:request.message];
        }
    } failure:nil];

}

- (void)startCountdownTimerIfNecessary
{
    if (self.countdownTimer) {
        return;
    }
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdownTimerDidFire:) userInfo:nil repeats:YES];
    self.codeButton.enabled = NO;
    self.codeButton.tag = 60;
    [self.codeButton setTitle:@"60s后重发" forState:UIControlStateDisabled];
    self.countdownTimer = timer;
}

- (void)countdownTimerDidFire:(NSTimer *)sender
{
    NSInteger seconds = self.codeButton.tag - 1;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%zds后重发",seconds] forState:UIControlStateDisabled];
    self.codeButton.tag = seconds;
    if (self.codeButton.tag == 0) {
        self.codeButton.enabled = YES;
        [self cancelCountdownTimer];
    }
}

- (void)cancelCountdownTimer
{
    [self.countdownTimer invalidate];
    self.countdownTimer = nil;
    self.codeField.enabled = YES;
    [self.codeButton setTitle:@"重新获取" forState:UIControlStateNormal];
}

#pragma QMUITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string originalValue:(BOOL)originalValue
{
    self.codeButton.enabled = (textField.text.length > 2);
    return YES;
}

@end
